

//
//  ShopMallViewController.m
//  广告页
//
//  Created by YYY on 2017/2/20.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "ShopMallViewController.h"

//图片缓存
#import "UIImageView+WebCache.h"

#import "iCarousel.h"

#define ITEM_SPACING 200

@interface ShopMallViewController () <iCarouselDataSource,iCarouselDelegate>
{
    iCarousel *carousel;
    NSMutableArray *icarouselArray;
}

@property (nonatomic, assign) CGSize cardSize;

@end

@implementation ShopMallViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat cardWidth = [UIScreen mainScreen].bounds.size.width-120;
    self.cardSize = CGSizeMake(cardWidth, [UIScreen mainScreen].bounds.size.height-300);
    icarouselArray = [NSMutableArray arrayWithObjects:@"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2722582827,3890415943&fm=23&gp=0.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1488186334&di=1ece8612049264509dc3fb3e468fdde9&src=http://photocdn.sohu.com/20150915/mp31896366_1442286000542_2.jpeg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1488186334&di=1c172197dc3299090f3f34fc543c73aa&src=http://pic1.ipadown.com/imgs/20140319/1vhqevsbfuu.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1488186334&di=a8b5caa092b01eea0787b0d37f0b8d42&src=http://pic1.ipadown.com/imgs/20140319/ks3ckb5naeh.jpg",nil];

    [self initCarouselView];
    // Do any additional setup after loading the view.
}

-(void)initCarouselView
{
    carousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    //第几张图片显示在当前位置
    [carousel scrollToItemAtIndex:0 animated:NO];
    carousel.clipsToBounds = YES;
    carousel.dataSource = self;
    carousel.delegate = self;
    //一开始中心图偏移量
    //carousel.contentOffset = CGSizeMake(0, 0);
    //类似contentoffset
    //carousel.viewpointOffset = CGSizeMake(0, 0);
    //控制滑动切换图片减速的快慢  默认0.95
    //carousel.decelerationRate = 0.95;
    /*
     iCarouselTypeLinear = 0,
     iCarouselTypeRotary,
     iCarouselTypeInvertedRotary,
     iCarouselTypeCylinder,
     iCarouselTypeInvertedCylinder,
     iCarouselTypeWheel,
     iCarouselTypeInvertedWheel,
     iCarouselTypeCoverFlow,
     iCarouselTypeCoverFlow2,
     iCarouselTypeTimeMachine,
     iCarouselTypeInvertedTimeMachine,
     iCarouselTypeCustom
     */

    carousel.type = iCarouselTypeRotary;
    [self.view addSubview:carousel];
}

#pragma mark iCarousel delegate
//切换的图片张数
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return icarouselArray.count;
}
//返回要切换的图片
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view
{
    UIView *cardView = view;
    
    if ( !cardView )
    {
        cardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.cardSize.width, self.cardSize.height)];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:cardView.bounds];
        imageView.tag = 9090+index;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [imageView sd_setImageWithURL:[NSURL URLWithString:icarouselArray[index]] placeholderImage:[UIImage imageNamed:@"image0.jpg"]];
        [cardView addSubview:imageView];
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = imageView.bounds;
        layer.path = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:5.0f].CGPath;
        imageView.layer.mask = layer;
        
        cardView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:imageView.frame cornerRadius:5.0f].CGPath;
        cardView.layer.shadowRadius = 3.0f;
        cardView.layer.shadowColor = [UIColor blackColor].CGColor;
        cardView.layer.shadowOpacity = 0.5f;
        cardView.layer.shadowOffset = CGSizeMake(0, 0);
    }
    
    return cardView;
}

//当前屏幕显示几张item图片
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    return icarouselArray.count;
}
//item图片之间的间隔宽
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    return ITEM_SPACING;
}

//点击item
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"index===%ld",index);
    NSLog(@"点击item执行方法");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
